<?php

namespace app\admin\controller\datacenter;

use app\common\controller\Backend;

use think\Controller;
use think\Request;
use think\Db;
use app\admin\model\VOnlineinfo;
use function GuzzleHttp\json_decode;

/**
 * 
 *
 * @icon fa fa-circle-o
 */
class Onlineinfo extends Backend
{
    /**
     * VOnlineinfo模型对象
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('VOnlineinfo');

    }
    
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个方法
     * 因此在当前控制器中可不用编写增删改查的代码,如果需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    

    /**
     * 查看
     */
    public function index()
    {
        //按时间搜索
        $startime = $this->request->param('startime');
        $endtime = $this->request->param('endtime');
        
        if(empty($startime) && empty($endtime)){
            $startime = $this->getMilliDate(date('Y-m-d 0:0:0',strtotime('-30 days')));
            $endtime = $this->getMilliDate(date('Y-m-d 23:59:59',time()));
        }elseif(empty($startime) && !empty($endtime)){
            $endtime = $this->getMilliDate($endtime.' 23:59:59');
            $startime = $endtime-(86400*31*1000)+1000;
        }elseif(!empty($startime) && empty($endtime)){
            $startime = $this->getMilliDate($startime.' 0:0:0');
            $endtime = $startime+(86400*31*1000)-1000;
        }else{
            $startime = $this->getMilliDate($startime.' 0:0:0');
            $endtime = $this->getMilliDate($endtime.' 23:59:59');
            if(($endtime - $startime) >(86400*30*1000)){
                $this->error('最多查30天记录');
            }
        }
        
        $map['createtime'] = array('between', [$startime,$endtime]);
        
        //搜索30条
        $t_num = [];
        $list = $this->model->where($map)->order('createtime asc')->select();
        
        foreach ($list as $key=>$val){
            $a = json_decode($val['online'],true);//转数据
            $b = array_column($a, 'num');//人数一维数组
            $c = array_sum($b);//全天在线人数
            $t_num[date('m.d',$val['createtime']/1000)] = $c;//在线人数
        }
        
        $this->view->assign([
            't_num'          => $t_num,
            'is_mobile' => is_mobile()?:0,
            'endtime' => date('Y-m-d',$endtime/1000),
            'startime' => date('Y-m-d',$startime/1000),
        ]);
        
        return $this->view->fetch();
    }
    
    /**
     * 每天订单统计列表
     */
    public function lists(){
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        //如果发送的来源是Selectpage，则转发到Selectpage
        if ($this->request->request('pkey_name'))
        {
            return $this->selectpage();
        }
        list($where, $sort, $order, $offset, $limit) = $this->buildparams();
        
        //搜索条件
        $filter = $this->request->get("filter", '');
        $filter_arr = json_decode($filter,true);
        $map = [];
        foreach ($filter_arr as $key=>$val){
            if($key == 'createtime'){
                $time_arr = explode(',', $filter_arr['createtime']);
                $star = $this->getMilliDate($time_arr[0].' 0:0:0');
                $end = $this->getMilliDate($time_arr[1].' 23:59:59');
                if(empty($time_arr[0])){
                    $map['createtime'] = array('<=', $end);
                }elseif(empty($time_arr[1])){
                    $map['createtime'] = array('>=', $star);
                }else{
                    $map['createtime'] = array('between', [$star,$end]);
                }
            }else{
                $map[$key] = $val;
            }
        }
        
        $total = $this->model
        ->where($map)
        ->order($sort, $order)
        ->count();
    
        $list = $this->model
        ->where($map)
        ->order($sort, $order)
        ->limit($offset, $limit)
        ->select();

        foreach ($list as $key=>$val){
            $a = json_decode($val['online'],true);//转数据
            $b = array_column($a, 'num');//人数一维数组
            $list[$key]['cnum'] = array_sum($b);//全天在线人数
            $list[$key]['hnum'] = max($b);//最高在线人数
            $list[$key]['anum'] = sprintf("%.2f", $list[$key]['cnum']/count($b));//平均在线人数
        }
        
        //统计
        $result = array("total" => $total, "rows" => $list, "statistics"=>[]);
    
        return json($result);
    }

}
